package cn.portx.frame;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.bstek.bdf3.dorado.jpa.JpaUtil;
import com.bstek.bdf3.saas.SaasUtils;
import com.bstek.bdf3.saas.command.CommandNeedReturn;
import com.bstek.bdf3.security.ContextUtils;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import cn.portx.frame.entity.Company;
import cn.portx.frame.entity.MineNotice;
import cn.portx.frame.entity.Notice;
import cn.portx.frame.entity.UserEntity;

@Component
public class NoticePR {

	@DataProvider
	@Transactional
	public void loadNotice(Page<Notice> page, Criteria criteria) {
		JpaUtil.linq(Notice.class).equal("sender", ContextUtils.getLoginUsername()).where(criteria).desc("createDate").paging(page);
	}

	@Transactional
	public void remoteSaveNotice(Notice notice) {
		this.saveNotice(notice);
	}
	
	@DataResolver
	@Transactional
	public void saveNotice(Notice notice) {
		notice.setSender(StringUtils.defaultString(notice.getSender(), ContextUtils.getLoginUsername()));
		notice.setCreateDate(new Date());
		notice.setCreateUser(StringUtils.defaultString(notice.getCreateUser(), ContextUtils.getLoginUsername()));

		if (EntityUtils.isEntity(notice)) {
			JpaUtil.save(notice);
		} else {
			JpaUtil.persist(notice);
		}

		List<MineNotice> notices = new ArrayList<MineNotice>();
		List<String> receivers = this.getReceivers(notice);
		for (String receiver : receivers) {
			MineNotice n = new MineNotice();
			n.setContent(notice.getContent());
			n.setCreateDate(new Date());
			n.setCreateUser(notice.getCreateUser());
			n.setReceiver(receiver);
			n.setSender(notice.getSender());
			n.setRead(0);

			notices.add(n);
		}
		JpaUtil.persist(notices);
	}

	private List<String> getReceivers(Notice notice) {
		List<String> list = new ArrayList<String>();
		if (notice == null || notice.getIsAll()) {
			synchronized (SaasUtils.class) {
				return SaasUtils.doNonQuery(new CommandNeedReturn<List<String>>() {

					@Override
					public List<String> execute() {
						List<String> orgIds = JpaUtil.linq(Company.class, String.class).select("orgId").equal("enable", "1").list();
						List<String> users = JpaUtil.linq(UserEntity.class, String.class).select("username").in("orgId", orgIds.toArray()).list();
						if (users != null) {
							list.addAll(users);
						}
						return list;
					}

				});
			}
		} else {
			list.addAll(Arrays.asList(StringUtils.split(notice.getReceiver(), ",")));
		}
		return list;
	}

	@Expose
	@Transactional
	public void deleteNotice(List<String> ids) {
		JpaUtil.lind(Notice.class).in("id", ids.toArray()).delete();
	}

}
